package cn.com.taurus.controller.user;

import cn.com.taurus.bean.HomePageWebBean;
import cn.com.taurus.bean.MyPurseWebBean;
import cn.com.taurus.biz.*;
import cn.com.taurus.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zhy
 * @createTime 2021-7-7 20:43:26
 * @remark 用户交易控制
 * @version 1.0
 */
@Controller
public class MyBuyController {
    @Autowired
    MyPurseWebBeanBiz myPurseWebBeanBiz;
    @Autowired
    HomePageWebBeanBiz homePageWebBeanBiz;
    @Autowired
    LoginRegisterController loginRegisterController;
    @Autowired
    ClientBiz clientBiz;
    @Autowired
    ClientAccountBiz clientAccountBiz;
    @Autowired
    FundBiz fundBiz;
    @Autowired
    DealMessageBiz dealMessageBiz;
    @Autowired
    AutomaticPlanMessageBiz automaticPlanMessageBiz;

    ModelAndView modelAndView = new ModelAndView();
    AutomaticPlanMessage automaticPlanMessage;



    private Double buyMoney;
    private ClientAccount clientAccount;
    private DealMessage dealMessage;

    @RequestMapping(value = "/Anonymous/userBuy2")
    public ModelAndView userBuy2() {
        modelAndView.setViewName("major/myBuy2");
        return modelAndView;
    }
    @RequestMapping(value = "/Anonymous/userBuy3")
    public ModelAndView userBuy3() {
        modelAndView.setViewName("major/myBuy3");
        return modelAndView;
    }



    @RequestMapping(value = "/Anonymous/automaticWeb")
    public ModelAndView automaticWeb() {
        Integer caId = clientAccountBiz.selectCaIdByCid(loginRegisterController.getClient().cId);
        List<AutomaticPlanMessage> list = automaticPlanMessageBiz.queryAutomaticPlanMessageByCaId(caId);

        modelAndView.addObject("automaticData",list);
        modelAndView.addObject("myPurseData",myPurseWebBeanBiz.selectMyPurseWebData(loginRegisterController.getClient().cAccount));
        modelAndView.setViewName("major/Automatic");
        return modelAndView;
    }

    /**
     * 前端撤销定投
     * @param apmId
     * @return
     */
    @RequestMapping("/Anonymous/deleteAutomaticWeb")
    public ModelAndView deleteAutomaticWeb(Integer apmId){
        automaticPlanMessageBiz.deleteAutomaticPlanMessageByApmId(apmId);
        return automaticWeb();
    }

    @RequestMapping(value = "/Anonymous/addAutomaticWeb2")
    public ModelAndView automaticWeb2() {
        modelAndView.setViewName("major/addAutomatic2");
        return modelAndView;
    }
    /**
     * 申购结果页面
     * @return
     */
    @RequestMapping(value = "/Anonymous/addAutomaticWeb3")
    public ModelAndView addAutomatic3() {
        modelAndView.setViewName("major/addAutomatic3");
        return modelAndView;
    }
    @RequestMapping(value = "/Anonymous/addAutomaticWeb")
    public ModelAndView addAutomaticWeb(String fId) {
        Fund fund = fundBiz.selectFundByFid(fId);
        modelAndView.addObject("fund",fund);
        modelAndView.addObject("client",loginRegisterController.getClient());
        modelAndView.setViewName("major/addAutomatic");
        return modelAndView;
    }



    /**
     * 根据交易密码判断
     * @param buyPassword
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Anonymous/addAutomatic2")
    public String addAutomatic2(Integer buyPassword) {
        Integer buyPasswordNow = clientAccountBiz.selectBuyPasswordByCId(loginRegisterController.getClient().cId);

        System.out.println(buyPassword+" "+buyPasswordNow);

        //判断密码是否相等
        if (buyPassword.equals(buyPasswordNow)){
            //生成定投订单，加入数据库
            automaticPlanMessageBiz.addAutomaticPlanMessage(this.automaticPlanMessage);

            return "{\"url\":\"/Anonymous/addAutomaticWeb3\"}";
        }else {
            return "{\"url\":\"/Anonymous/addAutomaticWeb2\"}";
        }
    }



    @RequestMapping(value = "/Anonymous/addAutomatic")
    public ModelAndView addAutomatic(AutomaticPlanMessage automaticPlanMessage,String date,String day,String fName) {
        //将定投设置修改进对象date+","+day
        automaticPlanMessage.setApmCycle(date+","+day);
        //将客户账户编号 修改进对象 根据loginRegisterController
        Client client = loginRegisterController.getClient();
        Integer caId = clientAccountBiz.selectCaIdByCid(client.cId);
        //获取cid然后根据cid在 clientAccountBiz中获取caid，设置进去
        automaticPlanMessage.setCaId(caId);
        //设置时间为现在
        Date dateNow = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        automaticPlanMessage.setStartTime(sdf.format(dateNow.getTime()));
        System.out.println(automaticPlanMessage.toString());

        //将定投信息全局变量设值
        this.automaticPlanMessage=automaticPlanMessage;
        modelAndView.addObject("automaticData",automaticPlanMessage);
        modelAndView.addObject("fName",fName);
        modelAndView.addObject("cAccount",client.cAccount);
        modelAndView.setViewName("major/addAutomatic2");
        return modelAndView;
    }



    @RequestMapping(value = "/Anonymous/userBuy")
    public ModelAndView userBuy(String fId,String buyMoney){

        //获取基金信息
        Fund fund = fundBiz.selectFundByFid(fId);

        //获取用户信息
        Client client = loginRegisterController.getClient();
        if (client != null){
            modelAndView.addObject("fundData",fund);
            modelAndView.addObject("fundType",fundBiz.selectFundType(fId));
            modelAndView.addObject("clientData",loginRegisterController.getClient());
            modelAndView.addObject("buyMoney",Double.parseDouble(buyMoney));
            modelAndView.setViewName("major/myBuy");
            return modelAndView;
        }else {
            modelAndView.setViewName("major/fundLogin");
            return modelAndView;
        }
    }

    /**
     * 确认购买
     * @param cAccount 用户账户
     * @param fId 基金编号
     * @param buyMoney 购买金额
     * @return modelAndView
     */
    @RequestMapping(value = "/Anonymous/verifyBuy")
    public ModelAndView verifyBuy(String cAccount,String fId,String buyMoney,String serviceCharge){

        System.out.println(cAccount+" "+fId+" "+buyMoney);
        if (cAccount != null && fId != null && buyMoney != null && serviceCharge != null){
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(serviceCharge);
            //根据账户查询相应cId
            Integer cId = clientBiz.selectCIdBycAccount(cAccount);
            //根据cId查询相应caId
            Integer caId = clientAccountBiz.selectCaIdByCid(cId);
            //生成交易申请订单
            this.dealMessage = new DealMessage
                    (dealMessageBiz.selectDmIdValue(),caId,fId,Double.parseDouble(buyMoney)-Double.parseDouble(serviceCharge),
                            simpleDateFormat.format(date.getTime()),Double.parseDouble(serviceCharge));
            //将客户账户、购买金额对象放入全局
            clientAccount = clientAccountBiz.selectAllPurse(cId);
            this.buyMoney=Double.parseDouble(buyMoney);
            //添加对象
            modelAndView.addObject("cAccount",cAccount);
            modelAndView.addObject("fName",fundBiz.selectFundByFid(fId).getfName());
            modelAndView.addObject("dealMessageData",dealMessage);
            //切换到确认购买界面
            modelAndView.setViewName("major/myBuy2");
            return modelAndView;
        }else{
            //回到购买界面
            modelAndView.setViewName("major/myBuy");
            return modelAndView;
        }
    }

    /**
     * 判断交易密码
     * ajax要返回json
     * @param buyPassword
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Anonymous/possible")
    public String possible(String buyPassword){
        Client client = loginRegisterController.getClient();
        //查询出用户相应的账户密码
        Integer buyPasswordData = clientAccountBiz.selectBuyPasswordByCId(client.cId);
        System.err.println(buyPassword+" "+buyPasswordData);
        if (Integer.parseInt(buyPassword) == buyPasswordData){
            return "{\"url\":\"/Anonymous/buyResult\"}";
        }else {
            return "{\"url\":\"/Anonymous/userBuy2\"}";
        }
    }

    /**
     * 购买结果
     * @return
     */
    @RequestMapping(value = "/Anonymous/buyResult")
    public ModelAndView buyResult() {
        if (clientAccount.getMoneyBox() - buyMoney < 0){
            //购买失败，储蓄罐金额不足
            modelAndView.setViewName("major/myBuy3");

        }else {
            //交易申请对象
            modelAndView.addObject("dealMessageData",dealMessage);
            //根据交易申请表的fId获取fName
            String fName = fundBiz.selectFundByFid(dealMessage.getfId()).getfName();
            modelAndView.addObject("fName",fName);
            //根据loginRegisterController里的client获取cAccount
            String cAccount = loginRegisterController.getClient().getcAccount();
            modelAndView.addObject("cAccount",cAccount);

            //计算冻结金额  冻结金额+购买金额 - 手续费
            Double frozenMoney = clientAccount.getFrozenMoney()+buyMoney-dealMessage.getServiceCharge();
            //计算公募基金金额 - 手续费
            Double publicFunds = clientAccount.getPublicFunds()+buyMoney-dealMessage.getServiceCharge();
            //计算储蓄罐活期余额
            Double moneyBox = clientAccount.getMoneyBox()-buyMoney;

            clientAccount.setFrozenMoney(frozenMoney);
            clientAccount.setPublicFunds(publicFunds);
            clientAccount.setMoneyBox(moneyBox);


            clientAccountBiz.clientAccountUpdate(clientAccount);
            //插入交易申请
            dealMessageBiz.buyApplication(dealMessage);


            //购买成功，储蓄罐金额足
            modelAndView.setViewName("major/myBuy4");
        }
        return modelAndView;
    }

}
